package org.fastiot.gateway

import io.netty.channel.ChannelHandlerContext

final class SessionHolder {
    private static final Map<InetSocketAddress, ChannelHandlerContext> sessions = [:]

    static void register(InetSocketAddress ip, ChannelHandlerContext ctx) {
        sessions.put(ip, ctx)
    }

    static void cancel(InetSocketAddress ip) {
        sessions.remove(ip)
    }

    static void send(InetSocketAddress ip, org.fastiot.gateway.Message msg) {
        def session = sessions[ip]

        if (session) {// && session.channel().isOpen()
            session.pipeline().write(msg)
        }
    }
}
